<?php defined('BASEPATH') OR exit('No direct script access allowed');

if ( ! function_exists('list_profile') ) {
	//$folder = p | c
	function list_profile() {
		$ci = & get_instance();
		$ci->load->model('emp_profile/salary_m');
		//1. tao database emp_salary_2013 neu chua co
		$ci->salary_m->create_table($y = date("Y"));//date("Y")
		$profiles = new stdClass();
		$cache_profile = 'emp_profile/profile_';
		
		
		$ci->load->model('emp_profile/profile_m');
		$ci->load->library('kunkuncache');
		
		$file_cache = 'emp_profile/profiles';
		if(!$profiles = $ci->kunkuncache->get($file_cache)) 
		{
			$param['active'] = 1;
			$profiles = $ci->profile_m->get_all();
			$ci->kunkuncache->write($profiles,$file_cache);
			
			$ci->load->model('users/groups_m');
			$ci->load->model('users/list_m');
			if(!empty($profiles)){
				foreach($profiles as $item){ // lưu cache 1 nhan vien
					
					$groups = $ci->groups_m->get_all();
					$groups_select = array_for_select($groups,'id','description');
					$item->group = return_single($groups_select,$item->group_id);				
					$sex_select = $ci->list_m->sex_select;
					$item->sex = return_single($sex_select,$item->sex);
					$ci->kunkuncache->write($item,$cache_profile.$item->id);
				}
				
			}
		}
		if($profiles){
			//luu vao database emp_salary_2013
			//2. insert
			$ci->salary_m->check_and_insert();
			//$prof = $ci->profile_m->get_all();
		}
	}	
}
	//xoa cache profile
if ( ! function_exists('refresh_profile') ) {
	function refresh_profile() {
		$ci = & get_instance();
		$ci->load->library('kunkuncache');
		
		$folder = 'emp_profile';
		$ci->kunkuncache->delete_all($folder);
		list_profile();
	}
}
